package com.github.ginirohikocha.dbm.fragment;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.github.ginirohikocha.dbm.R;

public class OrderFragment extends Fragment {

    private Activity activity;

    private OrderWindowFragment orderWindowFragment;

    private TextView waiting_orders, accepted_orders, handed_over_orders;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        activity = (Activity) context;
    }

    private int currentFragment;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View order = inflater.inflate(R.layout.fragment_order, container, false);
        orderWindowFragment = OrderWindowFragment.newInstance(WAITING_ORDERS);
        getChildFragmentManager().beginTransaction()
                .add(R.id.order_container, orderWindowFragment)
                .commit();
        currentFragment = WAITING_ORDERS;
        /*
        * 页面切换
        * */
        waiting_orders = order.findViewById(R.id.waiting_orders);//待接单
        accepted_orders = order.findViewById(R.id.accepted_orders);//已接单
        handed_over_orders = order.findViewById(R.id.handed_over_orders);//已转交
        waiting_orders.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switchOrderPage(WAITING_ORDERS);
            }
        });
        accepted_orders.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switchOrderPage(ACCEPTED_ORDERS);
            }
        });
        handed_over_orders.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switchOrderPage(HANDED_OVER_ORDERS);
            }
        });
        return order;
    }

    public static OrderFragment newInstance() {
        
        Bundle args = new Bundle();
        
        OrderFragment fragment = new OrderFragment();
        fragment.setArguments(args);
        return fragment;
    }

    public static final int WAITING_ORDERS = 0;
    public static final int ACCEPTED_ORDERS = 1;
    public static final int HANDED_OVER_ORDERS = 2;

    private void switchOrderPage(int i) {
        waiting_orders.setTextColor(getResources().getColor(R.color.defaultText));
        waiting_orders.setBackground(null);
        accepted_orders.setTextColor(getResources().getColor(R.color.defaultText));
        accepted_orders.setBackground(null);
        handed_over_orders.setTextColor(getResources().getColor(R.color.defaultText));
        handed_over_orders.setBackground(null);
        TextView target = null;
        switch (i) {
            case WAITING_ORDERS:
                target = waiting_orders;
                if (currentFragment != WAITING_ORDERS) {
                    currentFragment = WAITING_ORDERS;
                }
                break;
            case ACCEPTED_ORDERS:
                target = accepted_orders;
                if (currentFragment != ACCEPTED_ORDERS) {
                    currentFragment = ACCEPTED_ORDERS;
                }
                break;
            case HANDED_OVER_ORDERS:
                target = handed_over_orders;
                if (currentFragment != HANDED_OVER_ORDERS) {
                    currentFragment = HANDED_OVER_ORDERS;
                }
                break;
        }
        orderWindowFragment = OrderWindowFragment.newInstance(currentFragment);
        getChildFragmentManager().beginTransaction()
                .replace(R.id.order_container, orderWindowFragment)
                .commit();
        target.setTextColor(getResources().getColor(R.color.white));
        target.setBackgroundColor(getResources().getColor(R.color.defaultButoon));
    }

    public void refresh() {
        orderWindowFragment.refresh();
    }
}
